<template>
	<div class="text-center">
		{{msgContent}}
	</div>
</template>

<script>

import { ipcApiRoute, specialIpcRoute } from '@/api/main';
import { ipc } from '@/utils/ipcRenderer';
import { signIn } from '@/api/api'
import { message } from 'ant-design-vue';
import { toRaw } from 'vue';

	export default {
		name: "start",
		data() {
			return {
        msgContent: '启动中...',
        views: [
          {
            type: 'web',
            content: 'https://www.bilibili.com/',
            windowName: 'window-web',
            windowTitle: 'bilibili'
          },
          {
            type: 'html',
            content: '/public/html/view_example.html',
            windowName: 'window-html',
            windowTitle: 'html window'
          },
          {
            type: 'vue',
            content: '#/special/subwindow',
            windowName: 'window-vue',
            windowTitle: 'vue window'
          },
          {
            type: 'extra',
            content: '/GUI/index.html',
            windowName: 'window-player',
            windowTitle: 'ProgramPlayer'
          },
        ],
        playerWindowId: null,
        timerHandler: null,
		playerCreating: false,
			}
		},
		mounted() {
      // 启动签到
      this.startSign();
      this.init();
		},
    beforeDestroy() {
      if (this.timerHandler){
        clearInterval(this.timerHandler); // 清除定时器
      }
    },
		methods: {
      init() {
        // nodejs消息
        const that = this;
        ipc.removeAllListeners(specialIpcRoute.programDownloaded);
        ipc.on(specialIpcRoute.programDownloaded, (event, arg) => {
          that.refreshPage();
        })
      },
      startSign() {
        let that = this;
        that.sign()
        // 请求签到接口
        // signIn().then(res => {
        //   if (res.code != 0){
        //     that.msgContent = (res.desc||res.msg)
        //     // 签到失败 30秒之后继续签到
        //     setTimeout(function() {
        //       that.startSign();
        //     }, 30*1000);
        //   } else {
        //     // 签到成功
        //     that.msgContent = "签到成功，即将跳转..."
        //     
        //     // 开始连接mq
        //     that.startAmqp(res.data.devicecode);
        //     // 跳转html
        //     that.jumpHtml(1)
        //   }
        // }).catch(error => {
        //   setTimeout(function() {
        //     that.startSign();
        //   }, 10*1000);
        // })
      },
      startAmqp(queueName) {
        ipc.invoke(ipcApiRoute.initQueue, queueName).then(res => {
          console.log(res)
        });
      },
      jumpHtml(index) {
        const that = this;
		this.playerCreating = true;
        ipc.invoke(ipcApiRoute.createWindow, toRaw(this.views[index])).then(r => {
          that.playerWindowId = r;
		  that.playerCreating = false;
          console.log("创建返回的WinId:" + r);
          that.msgContent = "正在播放..."
          if (that.timerHandler) {
            clearInterval(that.timerHandler);
          }
          that.timerHandler = setInterval(function() {
            that.checkPlayerTimer(index);
          }, 5 * 1000);
          // 截图上传
          setTimeout(function() {
              that.screenShot();
          }, 3 * 1000);
        })
      },
      sign() {
        let that = this;
        ipc.invoke(ipcApiRoute.signIn).then(r => {
          console.log("sign返回：" + r);
          let errMsg = '';
          if (r instanceof Object) {
            if (r.code == 0) {
              that.msgContent = "签到成功，即将打开播放器..."
              that.startAmqp(r.data.devicecode);
              that.checkLocalProgram();
              return;
            } else {
              errMsg = r.msg || r.desc
              console.log("签到失败:" + errMsg)
            }
          } else {
            errMsg = r;
            console.log("签到异常:" + errMsg)
          }
          that.msgContent = "签到失败，30秒后重试..."
          // 30秒再次签到
          setTimeout(function() {
            that.sign();
          }, 30*1000);
        })
		that.startPlay()
      },
	  startPlay() {
		this.checkPlayerTimer(3);
	  },
      checkPlayerTimer(index) {
        const that = this;
        ipc.invoke(ipcApiRoute.getWCid, toRaw(this.views[index].windowName)).then(r => {
		console.log("检查窗口返回ID："+r)
          if (that.playerWindowId == null || that.playerWindowId != r && that.playerCreating == false) {
            that.jumpHtml(3)
          }
        })
      },
      checkLocalProgram() {
        ipc.invoke(ipcApiRoute.checkLocalProgram).then(r => {
          console.log("checkLocalProgram:"+r);
        })
      },
      refreshPage() {
        ipc.invoke(ipcApiRoute.refreshPage, this.playerWindowId).then(r => {
          console.log("refreshPage:"+r);
        })
      },
      screenShot() {
        ipc.invoke(ipcApiRoute.screenShot).then(r => {
          console.log("refreshPage:"+r);
        })
      }
		}

	};
</script>

<style lang="less" scoped>

.text-center{
  position:absolute;
  left:50%;
  top:50%;
  transform:translate(-50%,-50%);
  font-size: 50px;
}


</style>
